import matplotlib.pyplot as plt
from matplotlib.ticker import NullLocator
from typing import TYPE_CHECKING

from container import v_get_face_color_by_td
import visualizer.common as v

if TYPE_CHECKING:
    from puzzle.board import Board
    from util.prechecker import PrecheckResult
    from matplotlib.axes import Axes


def visualize_precheck_result(board: "Board", precheck_rst: "PrecheckResult") -> "Axes":
    fig, ax = plt.subplots()
    fig.canvas.manager.set_window_title(f"Precheck {precheck_rst.n_tiles}t Result") # type: ignore
    
    v.prepare_ax(ax)
    v.plot_board(ax, board)
    
    ax.xaxis.set_major_locator(NullLocator())
    ax.yaxis.set_major_locator(NullLocator())
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) # https://blog.csdn.net/jifaley/article/details/79687000
    plt.margins(0, 0)
    
    for ts in precheck_rst.dead_tiles:
        for td in ts:
            v.draw_polygon(
                ax,
                td.vertices,
                v_get_face_color_by_td(td),
                edgecolor = (1, 0, 0),
                linewidth = 2.0
            )

    fig.savefig(board.name, dpi = 300, transparent = True, pad_inches = 0)
    return ax